<?php
session_start();
//Create by Nguyen Hai Dang 20/09/2012
require 'globals.php';
require 'oauth_helper.php';
require_once('../../../../../config.php');

// Fill in the next 3 variables.
$guid = $_GET['yahoo_guide'];
$access_token = $_GET['acc_token'];
$access_token_secret = $_GET['acc_secret_token'];


// Call Contact API
$retarr = callcontact(OAUTH_CONSUMER_KEY, OAUTH_CONSUMER_SECRET,
                      $guid, $access_token, $access_token_secret,
                      false, true);

list($info, $header, $body) = $retarr;


if(!empty($body))
{
	
		
	$link = mysql_connect(DB_HOSTNAME,DB_USERNAME , DB_PASSWORD);
	if(!$link)
		die('Not connected : ' . mysql_error());
	
	$db_selected = mysql_select_db(DB_DATABASE, $link);
		
	if (!$db_selected)
		die('Can\'t select : ' . mysql_error());
	
	$mainemail = $_SESSION['mainemail'];
	$compare=mysql_query("SELECT name FROM email_lists WHERE name='$mainemail'");
	
	if(!mysql_num_rows($compare)) 
		mysql_query("INSERT INTO email_lists (name,format,notifyowner,visiblefields,ownerid) VALUES ('$mainemail','b','1','emailaddress','1')");
	
	$listid=mysql_query("SELECT listid FROM email_lists WHERE name='$mainemail'");
	
	if (mysql_num_rows($listid))
	while($row = mysql_fetch_array($listid)) $id=$row['listid'];

		$token = '"';
		for( $i = 0; $i < strlen($body); ++$i) {
			if( $body[$i] == "@" ) {
				$j = $i;
				while( $body[$j] !== $token )
					$j = $j - 1;
				$a = $j + 1;
				$j = $i;
				while( $body[$j] !== $token )
					$j = $j + 1;
				$b = $j - 1;
				$temp = "";
				for($j = $a; $j <= $b; ++$j) {
					$temp = $temp . $body[$j];
				}
			//	mysql_query("INSERT INTO email_list_subscribers (listid,confirmcode,emailaddress) VALUES ('$id','abc','$temp')");
				mysql_query("INSERT INTO email_list_subscribers (listid,emailaddress,format,confirmed,confirmcode) VALUES ('$id','$temp','h','1','8b69e28c1a48e5c35ca9c0865ee0034f')");
			}
		}	
	
    $sql1_total = mysql_query("SELECT emailaddress FROM email_list_subscribers WHERE listid='$id'");
    $numRows_total1 = mysql_num_rows($sql1_total);
	$sql2_total = mysql_query("UPDATE email_lists SET subscribecount=$numRows_total1, agreedelete=$numRows_total1  WHERE listid='$id'");
 
	mysql_close($link);
	header('Location:'.HTTP_SERVER. 'index.php');
}

/**
 * Call the Yahoo Contact API
 * @param string $consumer_key obtained when you registered your app
 * @param string $consumer_secret obtained when you registered your app
 * @param string $guid obtained from getacctok
 * @param string $access_token obtained from getacctok
 * @param string $access_token_secret obtained from getacctok
 * @param bool $usePost use HTTP POST instead of GET
 * @param bool $passOAuthInHeader pass the OAuth credentials in HTTP header
 * @return response string with token or empty array on error
 */
function callcontact($consumer_key, $consumer_secret, $guid, $access_token, $access_token_secret, $usePost=false, $passOAuthInHeader=true)
{
  $retarr = array();  // return value
  $response = array();

  $url = 'http://social.yahooapis.com/v1/user/' . $guid . '/contacts;count=1000';
  $params['format'] = 'json';
  $params['view'] = 'compact';
  $params['oauth_version'] = '1.0';
  $params['oauth_nonce'] = mt_rand();
  $params['oauth_timestamp'] = time();
  $params['oauth_consumer_key'] = $consumer_key;
  $params['oauth_token'] = $access_token;

  // compute hmac-sha1 signature and add it to the params list
  $params['oauth_signature_method'] = 'HMAC-SHA1';
  $params['oauth_signature'] =
      oauth_compute_hmac_sig($usePost? 'POST' : 'GET', $url, $params,
                             $consumer_secret, $access_token_secret);

  // Pass OAuth credentials in a separate header or in the query string
  if ($passOAuthInHeader) {
    $query_parameter_string = oauth_http_build_query($params, true);
    $header = build_oauth_header($params, "yahooapis.com");
    $headers[] = $header;
  } else {
    $query_parameter_string = oauth_http_build_query($params);
  }

  // POST or GET the request
  if ($usePost) {
    $request_url = $url;
    logit("callcontact:INFO:request_url:$request_url");
    logit("callcontact:INFO:post_body:$query_parameter_string");
    $headers[] = 'Content-Type: application/x-www-form-urlencoded';
    $response = do_post($request_url, $query_parameter_string, 80, $headers);
  } else {
    $request_url = $url . ($query_parameter_string ?
                           ('?' . $query_parameter_string) : '' );
    logit("callcontact:INFO:request_url:$request_url");
    $response = do_get($request_url, 80, $headers);
  }

  // extract successful response
  if (! empty($response)) {
    list($info, $header, $body) = $response;
    if ($body) {
      logit("callcontact:INFO:response:");
      //print(json_pretty_print($body));
    }
    $retarr = $response;
  }

  return $retarr;
}
?>
